package org.dashboard.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang.ObjectUtils;
import org.dashboard.bean.AdPage;
import org.dashboard.bean.AdUserPage;
import org.dashboard.constant.Constant;
import org.dashboard.dao.AdPageMapper;
import org.dashboard.dao.AdUserPageMapper;
import org.dashboard.service.AdPageService;
import org.dashboard.service.AdUserPageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = AdUserPageService.class)
@CacheConfig(cacheManager = Constant.DEFAULT_CACHE_MANAGER)
public class AdUserPageServiceImpl implements AdUserPageService {
    private static final Logger logger = LoggerFactory.getLogger(AdUserPageServiceImpl.class);

    @Autowired
    AdPageService adPageService;

    @Autowired
    AdUserPageMapper adUserPageMapper;

    @Override
    public int deleteByPrimaryKey(Integer userPageId) {
        return adUserPageMapper.deleteByPrimaryKey(userPageId);
    }

    @Override
    public int insert(AdUserPage record) {
        adUserPageMapper.insert(record);
        return record.getUserPageId();
    }

    @Override
    public int insertSelective(AdUserPage record) {
        adUserPageMapper.insertSelective(record);
        return record.getUserPageId();
    }

    @Override
    public AdUserPage selectByPrimaryKey(Integer userPageId) {
        return adUserPageMapper.selectByPrimaryKey(userPageId);
    }

    @Override
    public int updateByPrimaryKeySelective(AdUserPage record) {
        return adUserPageMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKeyWithBLOBs(AdUserPage record) {
        return adUserPageMapper.updateByPrimaryKeyWithBLOBs(record);
    }

    @Override
    public int updateByPrimaryKey(AdUserPage record) {
        return adUserPageMapper.updateByPrimaryKey(record);
    }


    @Override
    public AdPage loadUserPage(Integer pageId, Integer userId) {
        AdPage adPage = adPageService.selectByPrimaryKey(pageId);
        AdPage clonePage = (AdPage) ObjectUtils.clone(adPage);
        AdUserPage adUserPage = adUserPageMapper.selectByUserIdPageId(userId, pageId);
        if (adUserPage != null) {
            clonePage.setPageData(adUserPage.getUserPageData());
        }
        return clonePage;
    }

    @Override
    public List<Map<String, Object>> loadUserRolePages(Integer userId, Integer resourceType) {
        return adUserPageMapper.loadUserRolePages(userId, resourceType);
    }


    @Override
    public AdUserPage addOrUpdateUserPage(Integer userId, Integer pageId, String pageData) {
        AdUserPage adUserPage = adUserPageMapper.selectByUserIdPageId(userId, pageId);
        if (adUserPage == null) {
            adUserPage = new AdUserPage();
            adUserPage.setUserId(userId);
            adUserPage.setPageId(pageId);
            adUserPage.setUserPageData(pageData);
            adUserPage.setPageVersion(1);
            adUserPage.setUserPageId(adUserPageMapper.insert(adUserPage));
        } else {
            adUserPage.setUserPageData(pageData);
            adUserPage.setPageVersion(adUserPage.getPageVersion() + 1);
            adUserPageMapper.updateByPrimaryKeyWithBLOBs(adUserPage);
        }
        return adUserPage;
    }

    @Override
    public AdPage restoreDefaultUserPage(Integer userId, Integer pageId) {
        AdUserPage adUserPage = adUserPageMapper.selectByUserIdPageId(userId, pageId);
        if (adUserPage != null)
            adUserPageMapper.deleteByPrimaryKey(adUserPage.getUserPageId());
        return adPageService.selectByPrimaryKey(pageId);
    }

}
